﻿using System;
using Xpus.Website.Utilities;

namespace Xpus.Website.Data
{
	/// <summary>
	/// 用于方便地抛异常。
	/// </summary>
	public static class ExceptionHelper
	{
		/// <summary>
		/// 抛出字段为空的异常。
		/// </summary>
		/// <param name="field">字段名字。</param>
		public static void ThrowFieldNullException(string field)
		{
			throw new ArgumentNullException(
				field,
				string.Format(LanguageUtility.GetString("'{0}' cannot be null."), field));
		}

		/// <summary>
		/// 抛出字段长度（字符数）不符合要求的异常。
		/// </summary>
		/// <param name="field">字段名字。</param>
		/// <param name="minLength">字段长度最小值。</param>
		/// <param name="maxLength">字段长度最大值。</param>
		public static void ThrowFieldLengthException(string field, int minLength, int maxLength)
		{
			throw new ArgumentException(
				string.Format(LanguageUtility.GetString("The length of '{0}' must be between {1} and {2} characters."),
				field, minLength, maxLength),
				field);
		}

		/// <summary>
		/// 抛出字段长度（字符数）过长时的异常。
		/// </summary>
		/// <param name="field">字段名字。</param>
		/// <param name="maxLength">字段长度最大值。</param>
		public static void ThrowFieldTooLongException(string field, int maxLength)
		{
			throw new ArgumentException(
				string.Format(LanguageUtility.GetString("The length of '{0}' must be less than {1} characters."),
				field, maxLength),
				field);
		}

		/// <summary>
		/// 抛出字段长度（字符数）过短时的异常。
		/// </summary>
		/// <param name="field">字段名字。</param>
		/// <param name="minLength">字段长度最小值。</param>
		public static void ThrowFieldTooShortException(string field, int minLength)
		{
			throw new ArgumentException(
				string.Format(LanguageUtility.GetString("The length of '{0}' must be more than {1} characters."),
				field, minLength),
				field);
		}
	}
}
